package com.gavindon.android.adapters

import android.view.FocusFinder
import android.view.KeyEvent
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.airbnb.lottie.LottieAnimationView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.gavindon.android.R
import com.gavindon.android.entity.VideoTreeResp
import com.gavindon.android.utils.CourseFreeStatus
import com.gavindon.android.utils.FreeStatus
import com.gavindon.android.view.video.SaveCurrentView

/**
 * description:
 * Created by liNan on  2022-2-10 14:49
 */
class VideoChapterAdapter(layoutResId: Int, data: MutableList<VideoTreeResp>?, tabIndex: Int) :
    BaseQuickAdapter<VideoTreeResp, BaseViewHolder>(layoutResId, data) {
    val tabPos = tabIndex
    override fun convert(holder: BaseViewHolder, item: VideoTreeResp) {
        val chapter = holder.getView<TextView>(R.id.ada_tv_chapter)
        val chapterName = item.chapterName ?: ""
        val catalogName = item.catalogName ?: ""
        //如果章节和课程标题一样 只显示一个
        val showName = if (chapterName == catalogName) catalogName else chapterName + catalogName
        chapter.text = showName
        holder.setText(R.id.tv_chapter_duration, "${item.videoDuration ?: 0}分钟")

        //如果整个课程是免费的 不需要在章节课程上显示付费或者试看标志
        if (item.isFree != CourseFreeStatus.FREE.ordinal.toString()) {
            //freeViewing 0 付费 1可试看
            holder.setImageResource(
                R.id.iv_chapter_free,
                if (item.freeViewing == FreeStatus.FREE.ordinal.toString()) R.mipmap.icon23 else R.mipmap.icon22
            )
        }
        //控制是否显示播放按钮
        if (item.isPlay) {
            val playView = holder.getView<LottieAnimationView>(R.id.play_anim)
            playView.run {
                visibility = View.VISIBLE
                setAnimation(R.raw.video_playing)
                playView.postDelayed({playAnimation()},200)
//                playAnimation()
            }
        } else {
            val playView = holder.getView<LottieAnimationView>(R.id.play_anim)
            playView.run {
                cancelAnimation()
                frame = 0
                visibility = View.GONE
            }
        }

        holder.itemView.setOnKeyListener { v, keyCode, event ->
            if (event.action == KeyEvent.ACTION_DOWN) {
                if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
                    val finder = FocusFinder.getInstance()
                    val view = finder.findNextFocus(v.parent as ViewGroup, v, View.FOCUS_UP)
                    if (view == null) {
                        dpadListener?.invoke()
                        return@setOnKeyListener true
                    }
                }
            }

            return@setOnKeyListener false
        }

        //如果有viewPager 则默认只显示第一个viewpager
        if (tabPos == 0 && data.indexOf(item) == 0) {
            //默认播放第一个item的动画
            /*val initPlayAnim = holder.getView<LottieAnimationView>(R.id.play_anim)
            initPlayAnim.setAnimation(R.raw.video_playing)
            initPlayAnim.playAnimation()
            SaveCurrentView.currentView = initPlayAnim*/
        }
    }

    //按上键监听
    private var dpadListener: (() -> Unit)? = null

    @JvmName("setDpadListener1")
    fun setDpadListener(listener: () -> Unit) {
        this.dpadListener = listener
    }

    //获取adapter的第一个item lottieView
    private var viewListener: ((LottieAnimationView) -> Unit)? = null
    fun getItemViewListener(viewListener: (LottieAnimationView) -> Unit) {
        this.viewListener = viewListener
    }
}